package models;

import Dao.DBConnect;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class OrderModel {

    int id;
    String odid;
    int cus_id;
    String order_status;

    public OrderModel() {
//        conn = new DBConnect();
    }

    String order_date;
    int p_num;
    int car_id;
    String product_name;



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOdid() {
        return odid;
    }

    public void setOdid(String odid) {
        this.odid = odid;
    }

    public int getCus_id() {
        return cus_id;
    }

    public void setCus_id(int cus_id) {
        this.cus_id = cus_id;
    }

    public String getOrder_status() {
        return order_status;
    }

    public void setOrder_status(String order_status) {
        this.order_status = order_status;
    }



    public String getOrder_date() {
        return order_date;
    }

    public void setOrder_date(String order_date) {
        this.order_date = order_date;
    }

    public int getP_num() {
        return p_num;
    }

    public void setP_num(int p_num) {
        this.p_num = p_num;
    }

    public int getCar_id() {
        return car_id;
    }

    public void setCar_id(int car_id) {
        this.car_id = car_id;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }

    DBConnect conn = null;
    Statement stmt = null;

    @Override
    public String toString() {
        return "OrderModel{" +
                "id=" + id +
                ", odid='" + odid + '\'' +
                ", cus_id=" + cus_id +
                ", order_status='" + order_status + '\'' +
                ", order_date='" + order_date + '\'' +
                ", p_num=" + p_num +
                ", car_id=" + car_id +
                ", product_name='" + product_name + '\'' +
                '}';
    }


    public void insertRecord(int cid, int num,int pro_id) {

        try {
//			setCid(cid);
            // Execute a query
            System.out.println("Inserting record into the order table...");
            stmt = DBConnect.getConnection().createStatement();
            String sql = null;
            String sql2 = null;
            System.out.println(cid + "  "  + num + "  " + pro_id);
            // Include data to the database table
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
            LocalDateTime now = LocalDateTime.now();
            System.out.println(dtf.format(now));
            UUID uuid1 = UUID.randomUUID();
            String idcache = String.valueOf(uuid1);
            System.out.println("UUID : "+uuid1);
            sql = " insert into mian_wang_order(cus_id, order_status,order_date,odid) values('" + cid  + "', 'New ',' " + dtf.format(now) +" ', '" +  idcache + "')";

            stmt.executeUpdate(sql);

            sql2 = "insert into mian_wang_order_detail (odid,car_id,p_num) values ('"+ idcache +"',' " + pro_id + "','"+ num  +"')";
            stmt.executeUpdate(sql2);
//			conn.getConnection().close();

//			System.out.println("Balance inserted $" + bal + " for ClientID " + cid);

        } catch (SQLException se) {
            se.printStackTrace();
        }
    }

    public List<OrderModel> getAll(int cid) {
        List<OrderModel> orders = new ArrayList<>();
        String query = "select a.id,product_name,order_status,p_num,cus_id from mian_wang_order a ,mian_wang_order_detail b, mian_wang_product c\n" +
                "where a.odid = b.odid and b.car_id = c.id and cus_id = ? and order_status not in ('Cancel by User','Cancel by Sales','Deliver Car') ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);
            statement.setInt(1,cid);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                OrderModel order = new OrderModel();
                // grab record data by table field name into ClientModel account object
                order.setId(resultSet.getInt("id"));
                order.setProduct_name(resultSet.getString("product_name"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setP_num(resultSet.getInt("p_num"));
                order.setCus_id(resultSet.getInt("cus_id"));
                orders.add(order); // add account data to arraylist
            }
        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(orders.toString());
        return orders; // return arraylist
    }


    public List<OrderModel> getHis(int cid) {
        List<OrderModel> orders = new ArrayList<>();
        String query = "select a.id,product_name,order_status,p_num,cus_id from mian_wang_order a ,mian_wang_order_detail b, mian_wang_product c\n" +
                "where a.odid = b.odid and b.car_id = c.id and cus_id = ? and order_status in ('Cancel by User' ,'Cancel by Sales','Deliver Car') ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);
            statement.setInt(1,cid);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                OrderModel order = new OrderModel();
                // grab record data by table field name into ClientModel account object
                order.setId(resultSet.getInt("id"));
                order.setProduct_name(resultSet.getString("product_name"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setP_num(resultSet.getInt("p_num"));
                order.setCus_id(resultSet.getInt("cus_id"));
                orders.add(order); // add account data to arraylist
            }
//            connection.close();
        } catch (SQLException e) {

            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(orders.toString());
        return orders; // return arraylist
    }


    public List<OrderModel> s_getHisOrders() {
        List<OrderModel> orders = new ArrayList<>();
        String query = "select a.id,product_name,order_status,p_num,cus_id from mian_wang_order a ,mian_wang_order_detail b, mian_wang_product c\n" +
                "where a.odid = b.odid and b.car_id = c.id and order_status in ('Cancel by User' ,'Cancel by Sales','Deliver Car') ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);
//            statement.setInt(1,cid);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                OrderModel order = new OrderModel();
                // grab record data by table field name into ClientModel account object
                order.setId(resultSet.getInt("id"));
                order.setProduct_name(resultSet.getString("product_name"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setP_num(resultSet.getInt("p_num"));
                order.setCus_id(resultSet.getInt("cus_id"));
                orders.add(order); // add account data to arraylist
            }

        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(orders.toString());
        return orders; // return arraylist
    }

    public void deleteRecord(int oid) {


        try {
            System.out.println("Deleting record into the order table...");
//            stmt = conn.getConnection().createStatement();
            String sql = null;
//            String sql2 = null;
            System.out.println(oid + "  Order has been deleted  ");
            // Include data to the database table
            String Cancel1 = "Cancel";
            sql = " update mian_wang_order set  order_status = ? where id = ?";
            System.out.println(sql);
            try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
                statement.setString(1,"Cancel by User");
                statement.setInt(2, oid);
                System.out.println(statement.toString());
                statement.executeUpdate();
            }


            } catch (SQLException se) {
                se.printStackTrace();
            }

        }

    public void s_deleteRecord(int oid) {


        try {
            System.out.println("Deleting record into the order table...");
//            stmt = conn.getConnection().createStatement();
            String sql = null;
//            String sql2 = null;
            System.out.println(oid + "  Order has been deleted  ");
            // Include data to the database table
            String Cancel1 = "Cancel";
            sql = " update mian_wang_order set  order_status = ? where id = ?";
            System.out.println(sql);
            try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
                statement.setString(1,"Cancel by Sales");
                statement.setInt(2, oid);
                System.out.println(statement.toString());
                statement.executeUpdate();
            }


        } catch (SQLException se) {
            se.printStackTrace();
        }

    }


    public void editRecord(int oid,int pnum) {
        try {
        System.out.println("Update record in order table...");
//            stmt = conn.getConnection().createStatement();
        String sql = null;
//            String sql2 = null;
        System.out.println(oid + "  Order has been Update  ");
        // Include data to the database table
        String Cancel1 = "Cancel";
        sql = " update  mian_wang_order a ,mian_wang_order_detail b  set b.p_num= ? where a.odid = b.odid  and a.id = ?;\n";
        System.out.println(sql);
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
            statement.setInt(1,pnum);
            statement.setInt(2, oid);
            System.out.println(statement.toString());
            statement.executeUpdate();
        }


    } catch (SQLException se) {
        se.printStackTrace();
    }

    }

    public  List<OrderModel> s_getAllOrders() {
        List<OrderModel> orders = new ArrayList<>();
        String query = "select a.id,product_name,order_status,p_num,cus_id from mian_wang_order a ,mian_wang_order_detail b, mian_wang_product c\n" +
                "where a.odid = b.odid and b.car_id = c.id and  order_status not in ('Cancel by User' ,'Cancel by Sales','Deliver Car') ;";
        try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(query)) {
//            statement.setInt(1, cid);
//            statement.setInt(1,cid);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                OrderModel order = new OrderModel();
                // grab record data by table field name into ClientModel account object
                order.setId(resultSet.getInt("id"));
                order.setProduct_name(resultSet.getString("product_name"));
                order.setOrder_status(resultSet.getString("order_status"));
                order.setP_num(resultSet.getInt("p_num"));
                order.setCus_id(resultSet.getInt("cus_id"));
                orders.add(order); // add account data to arraylist
            }

        } catch (SQLException e) {
            System.out.println("Error fetching Accounts: " + e);
        }
        System.out.println(orders.toString());
        return orders; // return arraylist
    }

    public void s_editRecord(int oid) {
        try {
            System.out.println("Update record in order table...");
//            stmt = conn.getConnection().createStatement();
            String sql = null;
//            String sql2 = null;
            System.out.println(oid + "  Order has been Update  ");
            // Include data to the database table

            sql = " update  mian_wang_order a ,mian_wang_order_detail b  set a.order_status = 'Deliver Car' where a.odid = b.odid  and a.id = ?;\n";
            System.out.println(sql);
            try (PreparedStatement statement = DBConnect.getConnection().prepareStatement(sql)) {
//            statement.setInt(1, cid);
                statement.setInt(1, oid);
                System.out.println(statement.toString());
                statement.executeUpdate();
            }


        } catch (SQLException se) {
            se.printStackTrace();
        }

    }
}

